﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oca35
{
    public partial class SelectView : Form
    {

        public SelectView()
        {
            InitializeComponent();
        }

        private void SelectView_Load(object sender, EventArgs e)
        {
            //btnBegin.Enabled = false; Tiene que llenar las cajas de texto con el nombre de los jugadores
            cmbPlayers.SelectedIndex = 0;
            lbl3.Visible = false;
            lbl4.Visible = false;
            txtPlayer3.Visible = false;
            txtPlayer4.Visible = false;
            chkBox3.Visible = false;
            chkBox4.Visible = false;
        }

        private void btnBegin_Click_1(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                return;
            }

            var boardview = new BoardView();
            boardview.MdiParent = MdiParent;
            boardview.Players = GetInputPlayers();
            boardview.Show();
            this.Close();
        }

        private Player[] GetInputPlayers()
        {
            List<Player> players = new List<Player>();
            players.Add(new Player { Index = 0, Name = txtPlayer1.Text });
            players.Add(new Player { Index = 1, Name = txtPlayer2.Text, IsCom = chkBox2.Checked });

            if (cmbPlayers.SelectedIndex > 0)
            {
                players.Add(new Player { Index = 2, Name = txtPlayer3.Text, IsCom = chkBox3.Checked });
            }
            if (cmbPlayers.SelectedIndex > 1)
            {
                players.Add(new Player { Index = 3, Name = txtPlayer4.Text, IsCom = chkBox4.Checked });
            }

            return players.ToArray();
        }

        private void cmbPlayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbl3.Visible = cmbPlayers.SelectedIndex >= 1;
            txtPlayer3.Visible = cmbPlayers.SelectedIndex >= 1;
            lbl4.Visible = cmbPlayers.SelectedIndex >= 2;
            txtPlayer3.Visible = cmbPlayers.SelectedIndex >= 1;
            txtPlayer4.Visible = cmbPlayers.SelectedIndex >= 2;
            chkBox4.Visible = cmbPlayers.SelectedIndex >= 2;
            chkBox3.Visible = cmbPlayers.SelectedIndex >= 1;
        }

        public object Players { get; set; }

        private bool ValidateForm()
        {
            bool hasErrors = false;
            errProvider.Clear();

            if (String.IsNullOrWhiteSpace(txtPlayer1.Text))
            {
                errProvider.SetError(txtPlayer1, "Debe ingresar un nombre");
                hasErrors = true;
            }

            if (String.IsNullOrWhiteSpace(txtPlayer2.Text))
            {
                errProvider.SetError(txtPlayer2, "Debe ingresar un nombre");
                hasErrors = true;
            }
            if (cmbPlayers.SelectedIndex > 0)
            {
                if (String.IsNullOrWhiteSpace(txtPlayer3.Text))
                {
                    errProvider.SetError(txtPlayer3, "Debe ingresar un nombre");
                    hasErrors = true;
                }
            }

            if (cmbPlayers.SelectedIndex >1)
            {
                if (String.IsNullOrWhiteSpace(txtPlayer4.Text))
                {
                    errProvider.SetError(txtPlayer4, "Debe ingresar un nombre");
                    hasErrors = true;
                }
            }
            return hasErrors;
        }

   
    }
}
